<?php

namespace App\Plugins\Wechat;

use Illuminate\Support\Facades\Redis;

class Client
{
    static public function isActive($uin) {
        $channel = sprintf(SETTING::COMMAND_QUEUE, $uin, 'ping');
        $r = Redis::publish($channel, 'ping');
        return $r > 0;
    }

    static public function syncContacts($uin) {
        $key = sprintf(SETTING::CLIENT_INFO, $uin);
        $raw = Redis::hget($key, 'contacts');
        $data = json_decode($raw, 1);
        if ($data) {
            $ret_data = new \stdClass();
            $ret_data->contacts = collect($data['contacts']);
            $ret_data->groups = collect($data['groups']);
            return $ret_data;
        }
        return null;
    }

    static public function getUserInfo($uin) {
        $key = sprintf(SETTING::CLIENT_INFO, $uin);
        $raw = Redis::hget($key, 'User');
        if ($raw) {
            return json_decode(str_replace("'", '"', $raw));
        }
        return null;
    }
}